import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Dated {

    public static void main(String[] args) {
     
       System.out.println("============================== SimpleDateFormat ============================================");

        // timestamp 
        // System.out.println(System.currentTimeMillis());

        // timestamp --> format
        // Long timeStamp = System.currentTimeMillis(); 
        // SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // String sd = sdf.format(Long.parseLong(String.valueOf(timeStamp)));
        // String sd2 = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        // String sd3 = sdf.format(timeStamp);
        // System.out.println("格式化结果：" + sd);
        // System.out.println("格式化结果：" + sd2);
        // System.out.println("格式化结果：" + sd3);

        // timestamp --> format
        long tsp = System.currentTimeMillis();
        // Date d = new Date(tsp);
        // System.out.println(d); // Wed Sep 09 14:36:12 CST 2020

        var smp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // System.out.println(smp.format(d));
        // System.out.println(smp.format(tsp));

        // format --> timestamp
        String day = "2020-5-12 21:30:30";
        try{
         var sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         Date dat = sim.parse(day);
         long timestamp_d = dat.getTime() / 1000; 
         System.out.println(timestamp_d);
        }catch(ParseException e){
            System.out.println(e);
        }

        try{
            Long timestamped = new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-23").getTime() /1000 ;
            System.out.println("2020-04-23 timestamp is : " + timestamped);
        }catch(ParseException e){
            System.out.println(e);
        }


        // timestamp --> format
        Date date = new Date();
        SimpleDateFormat sdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d_str = sdt.format(date);
        // System.out.println(d_str);

        String datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println("today is : "+datetime);

        Date dd = Calendar.getInstance().getTime();
        // System.out.println(dd.toString());
        var sl = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // System.out.println(sl.format(dd));




        System.out.println("============================== LocalDateTime ============================================");




        LocalDate d = LocalDate.now();
        // System.out.println(d); // 2020-09-09

        LocalTime t = LocalTime.now();
        // System.out.println(t); //15:42:38.330053400

        LocalDateTime dt = LocalDateTime.now();
        // System.out.println(dt); //2020-09-09T15:42:38.330053400


        // LocalDateTime
        LocalDateTime dt2 = LocalDateTime.now();
        LocalDate d2 = dt2.toLocalDate(); // LocalDate
        LocalTime t2 = dt2.toLocalTime(); // LocalTime

        // System.out.println(dt2);
        // System.out.println(d2);
        // System.out.println(t2);


        // format --> LocalDateTime
        LocalDateTime dt3 = LocalDateTime.parse("2019-11-19T15:16:17");
        System.out.println(dt3.toLocalTime());
        LocalDate d3 = LocalDate.parse("2019-11-19");
        System.out.println(d3);
        LocalTime t3 = LocalTime.parse("15:16:17");
        System.out.println(t3);
        
        
        
        System.out.println("============================== ZonedDateTime ============================================");
       
        // first method
        ZonedDateTime zdt = ZonedDateTime.now(); // default
        System.out.println(zdt); // 2020-09-10T09:58:23.512741900+08:00[Asia/Shanghai]
        ZonedDateTime zdd = ZonedDateTime.now(ZoneId.of("America/New_York"));
        System.out.println(zdd); // 2020-09-09T21:59:06.429998600-04:00[America/New_York]

        // second method
        LocalDateTime ldt = LocalDateTime.of(2020, 9, 9, 10, 32, 10);

        ZonedDateTime zddt = ldt.atZone(ZoneId.systemDefault()); 
        System.out.println(zddt);// 2020-09-09T10:32:10+08:00[Asia/Shanghai]

        ZonedDateTime zddt2 = ldt.atZone(ZoneId.of("America/New_York")); 
        System.out.println(zddt2); // 2020-09-09T10:32:10-04:00[America/New_York]


        // US time transfor ZH 
        ZonedDateTime zdt_us = ZonedDateTime.now(ZoneId.of("America/New_York"));
        LocalDateTime ldt_zh = zdt_us.toLocalDateTime();
        System.out.println("ldt_zh datetime : "+ldt_zh);


        System.out.println("============================== DateTimeFormatter ============================================");
       

        // DateTimeFormatter
        ZonedDateTime zt = ZonedDateTime.now();
        String zts = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(zt);
        System.out.println(zts); // 2020-09-10 11:01:49

        String ztss = DateTimeFormatter.ofPattern("yyyy MMM dd EE HH:mm", Locale.CHINA).format(zt);
        System.out.println(ztss); 

        String ztUs = DateTimeFormatter.ofPattern("E, MMMM/dd/yyyy HH:mm", Locale.US).format(zt);
        System.out.println(ztUs); //Thu, September/10/2020 11:05


        System.out.println("============================== Instant ============================================");
       
        // Instant
        Instant ins = Instant.now();
        System.out.println(ins);  // 2020-09-10T03:21:39.228865800Z
        System.out.println(ins.getEpochSecond()); // 1599708445 s
        System.out.println(ins.toEpochMilli());   // 1599708445622 ms

        // + ZoneId --> ZonedDateTime
        //ZonedDateTime zod = ins.atZone(ZoneId.of("Asia/Shanghai"));
        ZonedDateTime zod = ins.atZone(ZoneId.systemDefault());
        System.out.println(zod);  //2020-09-10T11:23:17.357115400+08:00[Asia/Shanghai]


    }

}